package coom;


import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Response;

/**
 * Created by admin on 2018/5/19
 */

public class CreateInterceptor implements Interceptor {
    public static final int HTTP_CODE_ACCEPT = 201;
    public static final int HTTP_CODE_ERROR = 204;//请求成功，但没有处理

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());  //如果401了，会先执行TokenAuthenticator
        CreateInterceptorExceptioin interceptorExceptioin = new CreateInterceptorExceptioin();
        try {
            if (response.code() == HTTP_CODE_ACCEPT || response.code() == HTTP_CODE_ERROR) {
                interceptorExceptioin.setCode(response.code());
                interceptorExceptioin.setResponse(response);
            }
            return response;
        } catch (Exception e) {
            return null;
        }

    }


    public class CreateInterceptorExceptioin extends Error {
        private int code;
        private Response response;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public Response getResponse() {
            return response;
        }

        public void setResponse(Response response) {
            this.response = response;
        }
    }

}
